/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ClinicRecord;
import com.jims.master.modules.doctor.entity.ExamApply;
import com.jims.master.modules.doctor.entity.ExamItem;
import com.jims.master.modules.doctor.entity.HisDiagnosis;
import com.jims.master.modules.doctor.service.ClinicRecordService;
import com.jims.master.modules.doctor.service.ExamApplyService;
import com.jims.master.modules.doctor.service.ExamItemService;
import com.jims.master.modules.doctor.service.HisDiagnosisService;
import com.jims.master.modules.oper.entity.ClinicItemDict;
import com.jims.master.modules.oper.entity.ExamType;
import com.jims.master.modules.oper.service.ExamTypeService;
import com.jims.master.modules.orders.entity.Orders;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检查申请Controller
 * @author 陈晓阳
 * @version 2017-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/exam/examApply")
public class ExamApplyController extends BaseController {

	@Autowired
	private ExamApplyService examApplyService;
    @Autowired
    private ExamTypeService examTypeService;//检查项目业务
    @Autowired
    private ExamItemService examItemService;
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private HisDiagnosisService hisDiagnosisService;
    @Autowired
    private ClinicRecordService clinicRecordService;


    @ModelAttribute
    public ExamApply get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            return examApplyService.get(id);
        }else{
            return new ExamApply();
        }
    }
    /**
     * 检查申请主页面
     * @param model
     * @return
     * @Author 陈晓阳
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String index(ExamApply examApply,Model model,HttpServletRequest request, HttpServletResponse response) {
        UserUtils userUtils = new UserUtils();
  		//1 查询所有level=1检查项目
        ExamType examType= new ExamType();
        examType.setLevel("1");
        List<ExamType> examTypeList=this.examTypeService.findList(examType);
        model.addAttribute("examTypeList",examTypeList);
        String returnPath="";
        if(StringUtils.isNotBlank(examApply.getClinicId())){
            examApply.setInoroutFlag("0");
            List<HisDiagnosis> hisDiagnosisList = hisDiagnosisService.getByClinicId(examApply.getClinicId(),"");
            String count = "";
            String chineseMedicine = "";
            String westernMedicine = "";
            String hisDiagnosisDes1 = "";
            String hisDiagnosisDes2 = "";
            for(int i = 0; i<hisDiagnosisList.size(); i++){
                if("0".equals(hisDiagnosisList.get(i).getType())){
                    String diagType1 = "\r\n中医: \n";
                    hisDiagnosisDes1 +="      "+hisDiagnosisList.get(i).getIcdName()+";\n";
                    chineseMedicine = diagType1 +hisDiagnosisDes1;
                } else if("1".equals(hisDiagnosisList.get(i).getType())){
                    String diagType2 = "西医: \n";
                    hisDiagnosisDes2 +="      "+hisDiagnosisList.get(i).getIcdName()+";\n";
                    westernMedicine = diagType2 +hisDiagnosisDes2;
                }
                count = chineseMedicine +westernMedicine ;
            }
            ClinicRecord clinicRecord=clinicRecordService.getRecordByClinicId(examApply.getClinicId());
            String zhusu = clinicRecord!=null?clinicRecord.getZhusu():"";
            model.addAttribute("hisDiagnosisDes",count);
            model.addAttribute("zhusu",zhusu);
            model.addAttribute("loginName",userUtils.getUser().getId());
//            model.addAttribute("zhusu",zhusu);
            returnPath="modules/doctor/exam/examApplyIndex";
        }else{
            examApply.setInoroutFlag("1");
            returnPath="modules/doctor/exam/in/examApplyIndex";
        }
        ExamItem examItem=new ExamItem();
        examItem.setExamApply(examApply);

        model.addAttribute("examItem",examItem);
        model.addAttribute("examApply",examApply);
        Page<ExamApply> page = examApplyService.findPage(new Page<ExamApply>(request, response), examApply);
        model.addAttribute("page",page);
        return returnPath;
	}

    /**
     * 根据上级检查项目查询下级检查项目
     * @param examType
     * @param request
     * @param response
     * @return
     * @Author 陈晓阳
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"queryByPId", ""})
    @ResponseBody
    public List<ExamType> queryByPId(ExamType examType,HttpServletRequest request, HttpServletResponse response) {
        List<ExamType> list = examTypeService.findByPId(examType.getParent().getId());
         return list;
    }

    /**
     * 根据检查项目查询诊疗项目与执行科室
     * @param examType
     * @param request
     * @param response
     * @return
     * @Author 陈晓阳
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"queryByExam", ""})
    @ResponseBody
    public List<ClinicItemDict> queryByExam(ExamType examType,HttpServletRequest request, HttpServletResponse response) {
        return this.examTypeService.findByExam(examType.getId());
    }

    /**
     * 条件查询病人检查申请
     * @param model
     * @param request
     * @param response
     * @return
     * @Author 陈晓阳
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"queryByCondition", ""})
    public String queryByCondition(ExamApply examApply,Model model,HttpServletRequest request, HttpServletResponse response) {
        //病人Id 和检查项目名称
        Page<ExamApply> page = examApplyService.findPage(new Page<ExamApply>(request, response), examApply);
        model.addAttribute("page",page);
        if("0".equals(examApply.getInoroutFlag())){
            return "modules/doctor/exam/examApplyRight";
        }else{
            return "modules/doctor/exam/in/examApplyRight";
        }

    }
     /**
     * 保存检查申请
     * @param examItem
     * @param model
     * @return
      *@Author 陈晓阳
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExamItem examItem, Model model) {
	    StringData data=new StringData();
 		int i=examApplyService.saveExamApplyData(examItem);
        if(i>0){
            data.setCode("success");
            data.setData("保存检查申请成功");
        }else{
            data.setCode("error");
            data.setData("保存检查申请失败");
        }
        return data;
	}

    /**
     * 删除检查申请
     * @param examApply
     * @return
     * @Author 陈晓阳
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExamApply examApply) {
	    StringData data=new StringData();
 		int i=examApplyService.delete(examApply);
        if(i>0){
            data.setCode("success");
            data.setData("删除检查申请成功");
        }else{
            data.setCode("error");
            data.setData("删除检查申请失败");
        }
		return data;
	}

    /**
     * 检查确认页面数据
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "examConfirm")
    public String examConfirm(ExamApply examApply,Model model,HttpServletRequest request, HttpServletResponse response){
        if(examApply.getConfirmFlag()==null){
            examApply.setConfirmFlag("0");
        }
        if("0".equals(examApply.getInoroutFlag())){
            examApply.setChargeFlag("1");
        }
        Page<ExamApply> page =examApplyService.findPage(new Page<ExamApply>(request, response), examApply);
        model.addAttribute("page",page);
        model.addAttribute("examApply",examApply);
        return "modules/doctor/exam/examConfirm";
    }

    /**
     * 执行确认操作
     * @param examApply
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "examConfirmDo")
    @ResponseBody
    public StringData examConfirmDo(ExamApply examApply) {
        StringData data=new StringData();
        int i=examApplyService.examConfirmDo(examApply);
        if(i>0){
            data.setCode("success");
            data.setData("确认检查申请成功");
        }else{
            data.setCode("error");
            data.setData("确认检查申请失败");
        }
        return data;
    }

    /**
     * 取消确认操作
     * @param examApply
     * @return StringData
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "cancelExamConfirmDo")
    @ResponseBody
    public StringData cancelExamConfirmDo(ExamApply examApply){
        StringData data = new StringData();
        int i=examApplyService.cancelExamConfirm(examApply);
        if(i>0){
            data.setCode("success");
            data.setData("取消确认成功");
        }else{
            data.setCode("error");
            data.setData("取消确认失败");
        }
        return data;
    }
    /**
     * 检查确认页面数据
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "waitFeeData")
    public String waitFeeData(ExamApply examApply,Model model,HttpServletRequest request, HttpServletResponse response){
        examApply.setChargeFlag("0");
        examApply.setInoroutFlag("0");
        examApply.setImplementDept(UserUtils.getUser().getOrgDept().getId());
        Page<ExamApply> page =examApplyService.findPage(new Page<ExamApply>(request, response), examApply);
        model.addAttribute("page",page);
        model.addAttribute("examApply",examApply);
        return "modules/doctor/exam/examFeeData";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "printPresc")
    @ResponseBody
    public StringData printPresc(String clinicId,String examId,String patientId,String visitId){
        String printPath = examApplyService.printPresc(examId,clinicId,patientId,visitId);
        StringData stringData = new StringData();
        stringData.setData(printPath);
        stringData.setCode("success");
        return stringData;
    }

    /**
     * 住院申请退费
     * @param model
     * @return
     *@Author zp
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "outpRetCharge")
    @ResponseBody
    public StringData outpRetCharge(ExamApply examApply, Model model) {
        StringData data=new StringData();
        int i=examApplyService.inpRetCharge(examApply);
        if(i>0){
            data.setCode("success");
            data.setData("申请成功");
        }else{
            data.setCode("error");
            data.setData("申请失败");
        }
        return data;
    }


    /**
     * 住院确认退费
     * @param Orders
     * @param model
     * @param visitNo
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "retChargeOk")
    @ResponseBody
    public StringData retChargeOk(Orders orders, Model model, String visitNo) {
        StringData stringData = new StringData();
        int num = examApplyService.retChargeOk(orders);
        if(num>0){
            stringData.setCode("success");
            stringData.setData("退费成功");
        }else{
            stringData.setCode("error");
            stringData.setData("退费失败");
        }
        return stringData;


    }


 }